import loadingImg from "@/assets/loading.svg";
import styles from "./loading.module.less";

function createImg() {
    const img = document.createElement("img");
    img.dataset.role = "loading";
    img.src = loadingImg;
    img.classList.add(styles.loading);
    return img;
}

function getLoadingImg(el) {
    return el.querySelector("img[data-role=loading]");
}

export default function (el, binding) {
    const curImg = getLoadingImg(el);
    if(binding.value) {
        if(!curImg) {
            const img = createImg();
            el.appendChild(img);
        }
    } else {
        if(curImg) {
            curImg.remove();
        }
    }
}